package com.example.administrator.financialmanager.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.administrator.financialmanager.model.Tb_user;

/**
 * Created by Administrator on 2021/6/1.
 */

public class UserDao {
    private DBOpenHelper helper;
    private SQLiteDatabase db;

    public UserDao(Context context) {
        helper = new DBOpenHelper(context);
        db = helper.getWritableDatabase();
    }

    public void add(Tb_user user) {
        String sql = "insert into tb_user (username,password) values (?,?)";
        db.execSQL(sql,new Object[] {user.getUsername(),user.getPassword()});
    }
    /**
     * 设置密码信息
     *
     * @param tb_user
     */
    public void update(Tb_user tb_user) {
//		db = helper.getWritableDatabase();// 初始化SQLiteDatabase对象
        // 执行修改密码操作
        String sql = "update tb_user set password = ?";
        db.execSQL(sql,new Object[] { tb_user.getPassword() });
    }

    public boolean find(Tb_user user) {
        String sql = "select * from tb_user where username = ? and password = ?";
        Cursor cursor = db.rawQuery(sql, new String[]{user.getUsername(), user.getPassword()});
        if (cursor.moveToNext()) {
            return true;
        } else {
            return false;
        }
    }
    /**
     *
     */
    public long getCount() {
//		db = helper.getWritableDatabase();// 初始化SQLiteDatabase对象
        Cursor cursor = db.rawQuery("select count(password) from tb_user", null);// 获取密码信息的记录数
        if (cursor.moveToNext()){// 判断Cursor中是否有数据
            return cursor.getLong(0);// 返回总记录数
        }
        cursor.close();// 关闭游标
        return 0;// 如果没有数据，则返回0
    }
}
